-- UIIdcardRecord
-- Created by huanghr Oct/11/2016
-- 身份证登记

UIIdcardRecord = class("UIIdcardRecord", function()
    return cc.Layer:create();
end);

function UIIdcardRecord.create()
    return UIIdcardRecord.new();
end

-- 构造函数
function UIIdcardRecord:ctor()
    self:setName("UIIdcardRecord");
    local node = cc.CSLoader:createNode("layout/setting/PhoneNumber.csb");
    self:addChild(node);
    self.node = node;

    self:setLocalZOrder(UIMgr.BONUS_NOTIFY_ZORDER + 1);

    -- 注册事件处理回调函数
    self:registerEventCallback();

    -- 注册点击事件
    self:registerTouchEvent();

    -- 重绘
    self:redraw();

    -- 适配
    self:resize();
end

-- 适配
function UIIdcardRecord:resize()
    local node = self.node;

    -- bg居中显示并充满整个屏幕
    AlignM.fitToScreen(node, "bg");

    -- CT居中并缩放至合适大小
    AlignM.alignToCenter(node, "CT");

    -- BT置于底部并缩放至合适大小
    AlignM.alignToBottomCenter(node, "BT");
end

-- 注册事件处理回调函数
function UIIdcardRecord:registerEventCallback()
    self:registerScriptHandler(function(ev)
        if ev == "exit" then
            cclog("UIIdcardRecord 界面析构清理");
            EventMgr.removeAll("UIIdcardRecord");

            -- 显示系统设置界面
            local uiForm = UIMgr:getCurrentScene():getFormByName("UISystemSetting");
            if uiForm then
                uiForm:setVisible(true);
                playAppearEffect(uiForm);
            end
        elseif ev == "enter" then
            -- 隐藏掉系统设置界面
            local uiForm = UIMgr:getCurrentScene():getFormByName("UISystemSetting");
            if uiForm then
                uiForm:setVisible(false);
            end
        end
    end);
end

-- 注册点击事件
function UIIdcardRecord:registerTouchEvent()
    -- 注册返回按钮点击事件
    local btnBack = findChildByName(self.node, "BT/btn_back");
    local function onBackClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            AudioM.playFx("button_return");

            -- 关闭当前界面
            UIMgr.getCurrentScene():removeFormByName("UIIdcardRecord");
        end
    end
    AddBackEventHandler(self, btnBack, onBackClick);
    TextStyleM.setTextStyle(btnBack, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnBack:setTitleText(getLocStr("btn_text_back"));

    -- 发送验证码
    local CT = findChildByName(self.node, "CT");

    local btnAuthCode = findChildByName(CT, "auth_code/operate_btn");
    local function onSendCode(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            local str = self.editPhoneNum:getText();

            -- 去除前后空格
            self.idCardStr = string.trim(str);

            local ret = PhoneNumberRecordM.checkIdCard(self.idCardStr)
            if not ret then
                -- 身份证号非法
                alert(getLocStr("id_card_no_error"));

                return;
            end

            Operation.cmd_record_phone_number(self.idCardStr, PhoneNumberRecordM.OPERATE_RECORD_ID_CARD);
        end
    end

    btnAuthCode:addTouchEventListener(onSendCode);
end


-- 重绘
function UIIdcardRecord:redraw()
    -- 标题
    local CT = findChildByName(self.node, "CT");
    local titleLabel = findChildByName(CT, "bg/title");
    TextStyleM.setTitleStyle(titleLabel);
    titleLabel:setString(getLocStr("system_id_card_record"));

    titleLabel:setPositionY(titleLabel:getPositionY() - 20);

    -- 副标题
    local subTitleLabel = findChildByName(CT, "bg/subhead");
    subTitleLabel:setVisible(false);

    -- 步骤1:
    local authNode = findChildByName(CT, "auth_code");
    local inputLabel1 = findChildByName(authNode, "input_tip");


    local phoneNumberInput = findChildByName(authNode, "input_edit_box");
    -- 手机号码
    local editPhoneNum = ccui.EditBox:create(cc.size(300, 30), "images/ui/login/1.png");
    editPhoneNum:setAnchorPoint(cc.p(0, 0.5));
    editPhoneNum:setPosition(phoneNumberInput:getPosition());
    editPhoneNum:setFontName(getLocFontName());
    editPhoneNum:setFontSize(TextStyleM.TEXT_SIZE_SMALL2);
    editPhoneNum:setPlaceHolder(getLocStr("please_input_id_card"));
    editPhoneNum:setPlaceholderFontColor(TextStyleM.TEXT_COLOR_BEIGE);
    editPhoneNum:setPlaceholderFontSize(TextStyleM.TEXT_SIZE_SMALL2);
    editPhoneNum:setFontColor(TextStyleM.TEXT_COLOR_DARK_BROWN);
    editPhoneNum:setMaxLength(18);
    editPhoneNum:setInputMode(cc.EDITBOX_INPUT_MODE_SINGLELINE);
    self.editPhoneNum = editPhoneNum;
    phoneNumberInput:getParent():addChild(editPhoneNum);
    phoneNumberInput:setVisible(false);

    -- 记录身份证
    local btnOperate1 = findChildByName(authNode, "operate_btn");
    TextStyleM.setTextStyle(btnOperate1, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnOperate1:setTitleText(getLocStr("btn_text_submit_confirm"));

    -- 步骤2：(不显示)
    local bindNode = findChildByName(CT, "bind_node");
    bindNode:setVisible(false);
end